/*
 * @author Robert Hanwell
 * @version 3
 * @since January
 */

package com.smartbox.floodaction;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Dialog;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

// TODO: Auto-generated Javadoc
/**
 * The Class CreateEditCommunity.
 */
public class CreateEditCommunity extends BaseActivity implements
		OnItemSelectedListener {

	/** The region items. */
	private String[] regionItems;

	/** The organiser phone. */
	private EditText communityName, organiserName, organiserPhone;

	/** The sp region. */
	private Spinner spRegion;

	/** The organiser phone entry. */
	private String selectedRegion, communityNameEntry, organiserNameEntry,
			organiserPhoneEntry;

	/** The db helper. */
	private CommunityData dbHelper;

	/** The array adapter. */
	private ArrayAdapter<String> arrayAdapter;

	/** The region list. */
	private ArrayList<String> regionList;

	/** The d. */
	private Dialog d;

	/** The update flag. */
	private boolean updateFlag = false;

	/** The community. */
	private Community community;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.community);
		dbHelper = new CommunityData(this);
		dbHelper.open();
		d = new Dialog(this);

		initialize();
		addAdapter();
		spRegion.setOnItemSelectedListener(this);
		community = new Community();
		if (community.getId() > 0) {
			valuesToEdit();
			updateFlag = true;
		}

	}

	// pop menu out
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.smartbox.floodaction.BaseActivity#onCreateOptionsMenu(android.view
	 * .Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater menuInf = getMenuInflater();
		if (updateFlag) {
			menuInf.inflate(R.menu.update_menu, menu);
		} else {
			menuInf.inflate(R.menu.submit_menu, menu);
		}

		return true;
	}

	/**
	 * Values to edit.
	 */
	public void valuesToEdit() {

		communityName.append(community.getGroupName());
		organiserName.append(community.getOrganiserName());
		organiserPhone.append(community.getOrganiserPhone());

	}

	// UI items
	/**
	 * Initialize.
	 */
	public void initialize() {
		communityName = (EditText) findViewById(R.id.etCommunityName);
		organiserName = (EditText) findViewById(R.id.etOrganisersName);
		organiserPhone = (EditText) findViewById(R.id.etOrganisersPhone);
		spRegion = (Spinner) findViewById(R.id.spRegion);

	}

	/**
	 * Adds the adapter.
	 */
	public void addAdapter() {
		Resources aResource = this.getResources();
		regionItems = aResource.getStringArray(R.array.region);
		regionList = new ArrayList<String>(Arrays.asList(regionItems));
		// this is the drop down list which has pickup the color from the
		// string.xml
		arrayAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, regionList);
		// this is the pop out list
		arrayAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spRegion.setAdapter(arrayAdapter);

	}

	/**
	 * Validate.
	 * 
	 * @return true, if successful
	 */
	public boolean validate() {
		communityNameEntry = communityName.getText().toString();
		if (communityNameEntry.equalsIgnoreCase("")) {
			Toast.makeText(this, "Sorry you need to enter Community Name",
					Toast.LENGTH_LONG).show();
			return false;
		}
		organiserNameEntry = organiserName.getText().toString();
		if (organiserNameEntry.equalsIgnoreCase("")) {
			Toast.makeText(this, "Sorry you need to enter Organiser Name",
					Toast.LENGTH_LONG).show();
			return false;
		}
		organiserPhoneEntry = organiserPhone.getText().toString();
		if (organiserPhoneEntry.equalsIgnoreCase("")) {
			Toast.makeText(this,
					"Sorry you need to enter Organiser Phone Number",
					Toast.LENGTH_LONG).show();

			return false;
		}
		return true;
	}

	// hardkey menu options
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.bUpdate:
			if (validate()) {
				try {
					dbHelper.updateCommunityTable(community.getId(),
							communityNameEntry, organiserNameEntry,
							organiserPhoneEntry, selectedRegion);

					Toast.makeText(CreateEditCommunity.this, "Updated",
							Toast.LENGTH_SHORT).show();
					Intent i = new Intent("com.smartbox.floodaction.CAPTABBAR");
					startActivity(i);
					finish();
				} catch (Exception e) {

				} finally {

				}
			}
			return true;
		case R.id.bSubmit:
			if (validate()) {
				try {
					dbHelper.createEntryComunity(communityNameEntry,
							organiserNameEntry, organiserPhoneEntry,
							selectedRegion);
					Toast.makeText(CreateEditCommunity.this, "Submitted",
							Toast.LENGTH_SHORT).show();
				} catch (Exception e) {
					String error = e.toString();
					TextView tv = new TextView(this);
					tv.setText(error);
					d.setContentView(tv);
					d.show();
				} finally {
					Intent i = new Intent("com.smartbox.floodaction.CAPTABBAR");
					startActivity(i);
					finish();
				}
			}// end validate
			return true;
			// clear entry
		case R.id.bReset:

			return true;

		}

		return false;
	}

	// spinner select
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.widget.AdapterView.OnItemSelectedListener#onItemSelected(android
	 * .widget.AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		selectedRegion = regionList.get(arg2);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.widget.AdapterView.OnItemSelectedListener#onNothingSelected(android
	 * .widget.AdapterView)
	 */
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

	// after create start database connection
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		super.onStart();
		Log.i("onStart", "open database");
		dbHelper.open();

	}

	// before we close the screen close the database
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		super.onStop();
		Log.i("onStop", "closing database");
		dbHelper.close();

	}

}
